#include "fenetre.hpp"

Fenetre::Fenetre()
{
	compteurLignes = 0;	
	
	QVBoxLayout *layout = new QVBoxLayout();
	
	QHBoxLayout *layoutHote = new QHBoxLayout();
	QLabel *lhote = new QLabel("Hote :");
	hote = new QLineEdit("localhost");
	
	QHBoxLayout *layoutPort = new QHBoxLayout();
	QLabel *lport = new QLabel("Port :");
	port = new QSpinBox();
		port->setMinimum(10000);
		port->setMaximum(65535);
		
	lGrille = new QLabel("Grille :");
	int i=0;
	for (i=0; i<16; i++)
		grille[i].setText("");
		
	QHBoxLayout *ga = new QHBoxLayout();
		for (i=0; i<4; i++)
			ga->addWidget(&grille[i]);
	QHBoxLayout *gb = new QHBoxLayout();
		for (i=4; i<8; i++)
			gb->addWidget(&grille[i]);
	QHBoxLayout *gc = new QHBoxLayout();
		for (i=8; i<12; i++)
			gc->addWidget(&grille[i]);
	QHBoxLayout *gd = new QHBoxLayout();
		for (i=12; i<16; i++)
			gd->addWidget(&grille[i]);
		
	lEtat = new QLabel("");
	
	QPushButton *controle = new QPushButton("Prendre controle");
	
	QGridLayout *layoutMouvements = new QGridLayout;
	layoutMouvements->addWidget(&mouvements[0], 0, 1);
	layoutMouvements->addWidget(&mouvements[1], 1, 0);
	layoutMouvements->addWidget(&mouvements[2], 1, 1);
	layoutMouvements->addWidget(&mouvements[3], 1, 2);
	mouvements[0].setText("Haut"); mouvements[1].setText("Gauche");
	mouvements[2].setText("Bas"); mouvements[3].setText("Droite");
		
	
	QLabel *lLog = new QLabel("Log :");
	log = new QTextEdit();
		log->setReadOnly(true);
		
	layoutHote->addWidget(lhote); layoutHote->addWidget(hote);
	layoutPort->addWidget(lport); layoutPort->addWidget(port);
		
	lancer = new QPushButton("Lancer");
	
	layout->addLayout(layoutHote);
	layout->addLayout(layoutPort);
	layout->addWidget(lancer);
	layout->addWidget(lGrille);
	layout->addLayout(ga);
	layout->addLayout(gb);
	layout->addLayout(gc);
	layout->addLayout(gd);
	layout->addWidget(lEtat);
	layout->addWidget(controle);
	layout->addLayout(layoutMouvements);
	layout->addWidget(lLog);
	layout->addWidget(log);
	
	setLayout(layout);
	
	p = new QProcess();
	connect(lancer,SIGNAL(clicked()),this,SLOT(slotLancer()));
	connect(p,SIGNAL(readyReadStandardError()),this,SLOT(slotErreur()));
	connect(p,SIGNAL(readyRead()),this,SLOT(slotLire()));
	connect(controle,SIGNAL(clicked()),this,SLOT(slotControle()));
	
	connect(&mouvements[0],SIGNAL(clicked()),this,SLOT(slotHaut()));
	connect(&mouvements[1],SIGNAL(clicked()),this,SLOT(slotGauche()));
	connect(&mouvements[2],SIGNAL(clicked()),this,SLOT(slotBas()));
	connect(&mouvements[3],SIGNAL(clicked()),this,SLOT(slotDroite()));
}

void Fenetre::slotLancer()
{
	lancer->setEnabled(false);
	
	QString program = "../client/client";
	
	QFile f(program);
	if (!f.exists())
	{
		QMessageBox::critical(this, "ERREUR", "Pensez a d'abord compiler le client sans interface. Il doit etre present dans \"../client/\"");
		close();
	}
	
	QStringList arguments;
	arguments << hote->text() << QString::number(port->value());

	p->start(program,arguments);
}

void Fenetre::slotLire()
{
	QString l = p->readLine();
	while(l != "")
	{
		if (l.size() == 5)
		{	
			int i=compteurLignes;
			for (i=compteurLignes; i<compteurLignes+4; i++)
			{
				QString t = "" + l[i%4];
				grille[i].setText(t);
			}
			compteurLignes = compteurLignes+4;
			if (compteurLignes == 16)
				compteurLignes = 0;
		}
		else if (l.startsWith("Vous avez") || l.startsWith("Vous etes") || l.startsWith("Vous visualisez"))
		{
			lEtat->setText(l);
		}
		l = p->readLine();
	}
}

void Fenetre::slotErreur()
{
	QString e;
	e = "<span style=\"color : red\">ERREUR : <br/> " + p->readAllStandardError() + "</span>";
	log->append(e);
}

void Fenetre::slotControle()
{
	p->write("p\n");
}

void Fenetre::slotHaut() { p->write("h\n"); }
void Fenetre::slotBas() { p->write("b\n"); }
void Fenetre::slotGauche() { p->write("g\n"); }
void Fenetre::slotDroite() { p->write("d\n"); }

void Fenetre::closeEvent(QCloseEvent *event)
{
	p->write("q\n");
}
